﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.ComponentModel.Composition;

using NuMetaheuristics.Genotypes;
using NuMetaheuristics.Operators.Vector;
using NuMetaheuristics.Operators.Value;

namespace NuMetaheuristics.Contexts.Vector.Boolean
{
    [DataContract(Namespace = "http://numetaheuristics.codeplex.com/schemas")]
    [Export(typeof(IContext<BooleanVectorGenotype>))]
    public class LeadingOnesBlocks : BooleanVectorContext
    {
        [DataMember]
        private int _blockLength;

        public LeadingOnesBlocks()
            : base()
        {
        }

        public LeadingOnesBlocks(int length, int blockLength)
            : base(length)
        {
            _blockLength = blockLength;
        }

        override public void Evaluate(ref BooleanVectorGenotype genome)
        {
            int total = 0;

            for (int i = 0; i < genome.Vector.Length; i++)
            {
                if (genome.Vector[i])
                {
                    total++;
                }
                else
                {
                    break;
                }
            }
            total /= _blockLength;

            genome.Fitness = new FitnessBiggerIsBetter(total);
        }
    }
}
